from PIL import Image

# 读取图片
image = Image.open('milk.png')
width, height = image.size

# 检查图片尺寸是否为10x10的色块组成
if width != 512 or height != 512:
    print("图片尺寸不正确！")
    exit()

# 获取每个色块的尺寸
block_width = width // 10
block_height = height // 10

# 创建二维数组用于存储RGB值
rgb_led_data = [[[] for _ in range(10)] for _ in range(10)]

# 提取每个色块的RGB值
for i in range(10):
    for j in range(10):
        # 获取当前色块的位置
        x = j * block_width
        y = i * block_height

        # 获取当前色块的RGB值
        rgb = image.getpixel((x, y))

        # 存储RGB值到数组中
        rgb_led_data[i][j] = list(rgb)

# 生成C语言文件
with open('rgb_data.txt', 'w') as f:
    f.write("uint8_t rgb_led_data[10][10][3] = {\n")

    # 写入RGB值
    for i in range(10):
        f.write("  {")
        for j in range(10):
            f.write(" { %d, %d, %d }," % (rgb_led_data[i][j][0], rgb_led_data[i][j][1], rgb_led_data[i][j][2]))
        f.write(" },\n")

    f.write("};\n")
